package com.yw.admin.service;

import java.io.File;

import org.apache.log4j.Logger;

import com.yw.start.model.Model;
import com.yw.util.http.JettyFactory;


public class HTTPModule implements Model {
	Logger log = Logger.getLogger(HTTPModule.class);
	//Server server;

	// ServletContextHandler SLContextHandler;

	public void init() {
		log.debug("http module init");
		String os = System.getProperty("os.name");
		String fileDir = System.getProperty("user.dir") + "/webapps/resource";
		if (os.startsWith("win") || os.startsWith("Win")) {
			File dir = new File("x:/");
			if (dir.exists() || dir.isDirectory()) {
				if (!dir.canWrite()) {
					System.err.println("'x:/' Permissions is read-only");
				} else {
					fileDir = dir.getPath();
				}
			}
		} else {
			// linux dir
		}
		System.setProperty("oa.file.dir", fileDir);
		log.debug("fileDir is " + fileDir);
		
	}

	public void start() {

		String jettyPath = System.getProperty("user.dir")
				+ "/etc/jetty-spring.xml";		
		String[] config = new String[] { jettyPath };

		try {
			JettyFactory.getInstance().startService(config);
		} catch (Exception e) {
			e.printStackTrace();
		}

		/*
		 * SLContextHandler = new
		 * ServletContextHandler(ServletContextHandler.SESSIONS);
		 * SLContextHandler.setContextPath("/hello");
		 * SLContextHandler.setResourceBase(".");
		 * SLContextHandler.setClassLoader
		 * (Thread.currentThread().getContextClassLoader());
		 * SLContextHandler.addServlet(new ServletHolder(new
		 * DefaultServlet("Hello Kongxx!")), "/kongxx");
		 */
		log.debug("http module start");
	}

	public void stop() {
		log.debug("http module stop");

	}

	public void destroyed() {
		log.debug("http module destroyed");

	}

	public String getName() {
		return "jetty http module";
	}

}
